package com.ating.demo.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ating.demo.Entity.Department;
import com.ating.demo.Service.DepartmentService;
import com.ating.demo.common.Result;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**查询所有**/
    @GetMapping("/getAll")
    public Result getDepartment(Department department){
        List<Department> list = departmentService.selectAll(department);
        return Result.success(list);
    }

    /**查询当个**/
    @GetMapping("/getOne/{id}")
    public Result getOne(@PathVariable Integer id){
        Department department = departmentService.selectOne(id);
        return Result.success(department);
    }

    /**分页查询**/
    @GetMapping("/selectPage")
    public Result getPage(@RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          Department department){
        PageInfo<Department> pageInfo = departmentService.selectPage(pageSize,pageNum,department);
        return Result.success(pageInfo);
    }

    /**添加用户**/
    @PostMapping("/add")
    public Result add(@RequestBody Department department){
        departmentService.add(department);
        return Result.success();
    }

    /**更新用户**/
    @PutMapping("/update")
    public Result update(@RequestBody  Department department){
        departmentService.update(department);
        return Result.success();
    }

    /**删除**/
    @DeleteMapping("/delete/{id}")
    public Result move(@PathVariable Integer id){
        departmentService.moveById(id);
        return Result.success();
    }

    /**批量删除**/
    @DeleteMapping("/batch")
    public Result batch(@RequestBody List<Integer> ids){
        for(Integer id : ids){
            departmentService.moveById(id);
        }
        return Result.success();
    }

    /**导出数据**/
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Department> list = departmentService.selectAll(null);
        //写到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //设置中文表头
        writer.addHeaderAlias("name","姓名");
        //设置想导出的字段
        writer.setOnlyAlias(true);
        //写出数据
        writer.write(list,true);
        //设置输出文件名、文件头和格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String filename = URLEncoder.encode("部门信息", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename="+filename+".xlsx");
        //写出到输出流
        OutputStream os = response.getOutputStream();
        writer.flush(os);
        writer.close();
    }

    @PostMapping("/import")
    public Result importExcel(MultipartFile file) throws IOException {
        //输入文件
        InputStream is = file.getInputStream();
        //读文件
        ExcelReader reader = ExcelUtil.getReader(is);
        //读取里面的数据
        reader.addHeaderAlias("姓名","name");
        List<Department> list = reader.readAll(Department.class);
        //写入数据库
        for (Department department : list) {
            departmentService.add(department);
        }
        return Result.success();
    }
}
